package com.educate.controller;

import cn.hutool.core.map.MapUtil;
import com.educate.common.api.Result;
import com.educate.common.lang.Const;
import com.educate.pojo.Course;
import com.educate.service.CourseService;
import com.educate.util.OssFileUpload;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author yjj
 * @date 2021/11/22
 * @description: 课程Controller(teacher)
 */
@Api(tags = "老师课程接口")
@RestController
@RequestMapping("/course/tea")
public class TeacherCourseController {
    @Autowired
    private OssFileUpload ossFileUpload;

    @Autowired
    private CourseService courseService;
    /**
     * 新增课程
     */
    @PreAuthorize("hasAuthority('sys:teacher')")
    @ApiOperation("新增课程")
    @PostMapping("/add")
    public Result addCourse(@ApiParam(value = "新增课程信息json",required = true) @RequestBody @Validated Course course){
        String classId = courseService.addCourseCover(course);
        return Result.success(
                MapUtil.builder()
                        .put("classId",classId)
                        .build()
        );
    }
    /**
     * 上传课程封面
     */
    @ApiOperation("上传课程封面(jpg/png)")
    @PostMapping("/uploadCover")
    public Result uploadCover(MultipartFile file){
        return Result.success(ossFileUpload.ossUpload(file, Const.COVER_FOLDER));
    }

    /**
     * 获取我的课程(老师)
     * @return List<CourseTypeDto>
     */
    @ApiOperation("获取我的课程(老师)")
    @GetMapping("/myCourse")
    public Result getAllOnlineCourse(){
        return Result.success(courseService.getTeacherCourse());
    }
}
